iT邦幫忙

2025 iThome 鐵人賽

DAY 15
0
Rust

NautilusTrader 架構解析:Rust 在高效能量化交易平台中的角色與優勢系列 第 15

【Day15】- NautilusTrader 如何結合 Rust 和 Python

  • 分享至 

  • xImage
  •  

今天我們來說一下 NautilusTrader 是如何 結合 Rust 和 Python 的

可以透過查看 build.py 來得到,步驟如下

1. Rust Compilation

Rust 函式庫是使用 Cargo 編譯的,具有特定的功能標誌和最佳化設定檔。

意思是:你用 Rust 寫好程式後,會透過 cargo build 編譯出 .a(靜態庫)或 .so/.dylib/.dll(動態庫)。

這一段可以參考 build.py:146-194


2. C Header Generation

支援 FFI 的 Rust crate 使用 cbindgen 產生 C 標頭,然後 Cython 將其用於 FFI 整合。

因為 Cython 無法直接使用 Rust 的函式定義,它只能識別 C 的函式簽名。所以用 cbindgen 把 Rust 的函式輸出成 .h 檔:


3. Cython Compilation

Cython 擴充功能使用 NumPy 整合進行編譯,並與 Rust 靜態函式庫連結。

這表示你在寫 Cython 的 .pyx 時可能也用到 NumPy(如資料傳遞),而最重要的是:

  • Cython 會呼叫剛剛用 Rust 寫的函式(透過 C header)
  • 並在 setup.py 中設定讓 Cython 連結 Rust 產生的 .a 或 .so 靜態函式庫

這一段可以參考 build.py:279-291


4. PyO3 Library Building

PyO3 共享函式庫被建置並複製到 Python 套件結構中。

如果不是用 C header + Cython 的方式,而是使用 PyO3 這種更現代的整合方式,Rust 可以直接 export 為 Python 模組(不需要 C header),也是我們在 Day 5 所講的.

這會產生一個 .so 檔案(Python 模組),並複製到你開發的 Python 專案中。

這一段可以參考 build.py:331-338


5. Final Assembly

建置的延伸會複製回原始碼樹狀結構,以進行開發和輪子封裝。

意思是:上面用 maturin build 或 setup.py build_ext 產生的 .so / .pyd 模組,會被放回原始的 Python 套件結構中,像這樣:

project/
├── my_module/
│   ├── __init__.py
│   ├── my_module.cpython-38-x86_64-linux-gnu.so  <-- Rust/Cython 編譯結果

這樣 Python 就可以直接 import my_module 使用 Rust 加速過的模組了。

這一段可以參考 build.py:315-328


所以根據以上,我們知道有兩種方式

情境一:原本就有 Python + Cython 結構

Python ← Cython (.pyx) ← C header (.h) ← Rust

編譯流程:

  1. Rust 用 #[no_mangle] 和 extern "C" 暴露函數
  2. 用 cbindgen 產生 .h 檔(C header)
  3. Cython 用 cdef extern from "my_rust.h" 宣告那些函數
  4. 在 .pyx 檔中用 Cython 包裝 Rust 函數
  5. 用 setuptools 編譯並連結 .so 或 .a

情境二:只有純 Python 架構

Python ← PyO3 ← Rust

編譯流程:

  1. Rust 使用 PyO3 標註函數 #[pyfunction] / #[pymodule]
  2. 用 maturin 或 setuptools-rust 打包成 Python 原生模組 .so
  3. Python 直接 import 使用,不需要 .h 和 .pyx

所以我們自己想開發的話,應該要使用第二種方法會比較容易,明天會來說明一下,預計要開發什麼新的 crate.


上一篇
【Day14】- Rust(自動化測試)
下一篇
【Day16】- 推論 Crate
系列文
NautilusTrader 架構解析:Rust 在高效能量化交易平台中的角色與優勢22
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言